package main

import (
	"fmt"
	_ "gitee.com/forxing/gokb"
	"gitee.com/moralfly/beego-orm/orm"
	"strings"
)

type Table_demo struct {
	Id   string `json:"id" orm:"pk"`
	Name string `json:"name"`
	Sex  int32  `json:"sex"`
	Age  int32  `json:"age"`
}

func (t *Table_demo) Prefix() string {
	return strings.ToUpper(strings.TrimPrefix(t.TableName(), "tbl_"))
}

func (t *Table_demo) TableName() string {
	return "table_demo"
}

func init() {
	const (
		host     = "127.0.0.1"
		port     = 54321
		user     = "root"
		password = "123456"
		dbname   = "platform"
	)

	orm.RegisterDriver("kingbase", orm.DRKingBase)
	url := fmt.Sprintf("kingbase://%s:%s@%s:%d/%s?sslmode=disable", user, password, host, port, dbname)
	orm.RegisterDataBase("default", "kingbase", url)

	// 需要在init中注册定义的model
	orm.RegisterModel(new(Table_demo))
}

func main() {
	_ = KingBaseDelete()
	_ = KingBaseInsert()
	_ = KingBaseUpdate()
	_ = KingBaseTransaction()
	_ = KingBaseQuery()
}

func KingBaseTransaction() (err error) {
	var transactionHandler = orm.NewOrm()
	if err = transactionHandler.Begin(); err != nil {
		fmt.Println("ERROR: KingBaseTransaction(), 开启事务失败, err=", err)
		return
	}

	defer func() {
		if err != nil {
			_ = transactionHandler.Rollback()
		} else {
			_ = transactionHandler.Commit()
		}
	}()
	var demo = Table_demo{
		Id:   "2",
		Name: "2",
		Sex:  10,
		Age:  20,
	}
	_, err = transactionHandler.Insert(&demo)
	if err != nil {
		fmt.Println("Insert", err)
		return err
	}

	return nil
}

func KingBaseQuery() (err error) {
	var num int64
	var demo Table_demo
	err = orm.NewOrm().Raw(`select * from table_demo where id = '1' `).QueryRow(&demo)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(demo)

	// read one
	u := Table_demo{Id: "1"}
	err = orm.NewOrm().Read(&u)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("Read", u)

	var posts []*Table_demo
	num, err = orm.NewOrm().QueryTable(new(Table_demo)).Limit(10).All(&posts)
	if err != nil {
		fmt.Println(err)
	}

	fmt.Println("All", num, posts)
	for i, i2 := range posts {
		fmt.Println(i, i2)
	}

	var idList []string
	_, err = orm.NewOrm().Raw(`select id from table_demo limit 10 `).QueryRows(&idList)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(len(idList), idList)

	var maps []orm.Params
	num, err = orm.NewOrm().Raw("SELECT id FROM table_demo WHERE name = ?", "astaxie11").Values(&maps)
	if num > 0 {
		fmt.Println(maps[0]["id"])
	}
	fmt.Println("maps", num, maps, err)

	return nil
}

func KingBaseUpdate() (err error) {
	var demo = Table_demo{
		Id:   "1",
		Name: "astaxie11",
		Sex:  10,
		Age:  20,
	}
	_, err = orm.NewOrm().Update(&demo, []string{"name"}...)
	if err != nil {
		fmt.Println("Update Exec 1", err)
	}
	return nil
}

func KingBaseInsert() (err error) {
	_, err = orm.NewOrm().Raw(`INSERT INTO table_demo VALUES ('22', '苏茈茈', '2', '2')`).Exec()
	if err != nil {
		fmt.Println("Insert Exec 1", err)
	}

	var demo = Table_demo{
		Id:   "1",
		Name: "1",
		Sex:  10,
		Age:  20,
	}
	_, err = orm.NewOrm().Insert(&demo)
	if err != nil {
		fmt.Println("Insert", err)
	}

	var demoMulti = []Table_demo{{
		Id:   "1111",
		Name: "1111",
		Sex:  11,
		Age:  11,
	}, {
		Id:   "1122",
		Name: "1122",
		Sex:  22,
		Age:  22,
	}}

	_, err = orm.NewOrm().InsertMulti(len(demoMulti), &demoMulti)
	if err != nil {
		fmt.Println("InsertMulti", err)
	}
	return nil
}

func KingBaseDelete() (err error) {
	var num int64
	// delete
	num, err = orm.NewOrm().Delete(&Table_demo{Id: "1"})
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("Delete", num)

	_, err = orm.NewOrm().Raw(`DELETE FROM table_demo WHERE id like '%1%'`).Exec()
	if err != nil {
		fmt.Println("DELETE Exec 1", err)
	}
	return nil
}
